package com.ynashk.emd.datasource;

import org.springframework.core.NamedThreadLocal;

/**
 * @author user
 */
public class DataSourceNameContextHolder {

    private static final ThreadLocal<String> DATA_SOURCE_NAME_CONTEXT_HOLDER = new NamedThreadLocal<>("DataSourceContext");

    public static final String DEFAULT_DATASOURCE_NAME = "primary";

    public static void setDataSourceName(String dataSourceName) {
        DATA_SOURCE_NAME_CONTEXT_HOLDER.set(dataSourceName);
    }

    public static String getDataSourceName() {
        return DATA_SOURCE_NAME_CONTEXT_HOLDER.get() != null ? DATA_SOURCE_NAME_CONTEXT_HOLDER.get() : DEFAULT_DATASOURCE_NAME;
    }

    public static void resetDataSourceName() {
        DATA_SOURCE_NAME_CONTEXT_HOLDER.remove();
    }

}